package com.test.security.a;



import cn.hutool.core.codec.Base64;

import java.security.*;

/**
 * @author: zouren
 * @date: 2022/3/8
 * @description: 生成公钥私钥对
 */
public class KeyGenerater {
    private String priKey;
    private String pubKey;
    public void generater() {
        try {
            KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
            SecureRandom secrand = new SecureRandom();
            // 初始化随机产生器
            secrand.setSeed("wwasdfsdaf".getBytes());
            keygen.initialize(1024, secrand);
            KeyPair keys = keygen.genKeyPair();
            PublicKey pubkey = keys.getPublic();
            PrivateKey prikey = keys.getPrivate();
            pubKey = Base64.encode(pubkey.getEncoded());
            priKey = Base64.encode(prikey.getEncoded());
            System.out.println("pubKey = " + pubKey);
            System.out.println("priKey = " + priKey);
        } catch (java.lang.Exception e) {
            System.out.println("生成密钥对失败");
            e.printStackTrace();
        }
    }
    public String getPriKey() {
        return priKey;

    }

    public String getPubKey() {
        return pubKey;
    }
}
